import { NesUtil } from "..";
import { IRQType } from "../CPU";
import { Mappers0 } from "./Mappers0";

export class Mappers2 extends Mappers0 {
    constructor(nes: NesUtil) {
        super(nes);
    }

    write (address: number, value: number) {
        // Writes to addresses other than MMC registers are handled by NoMapper.
        if (address < 0x8000) {
            super.write.apply(this, arguments);
            return;
        }

        else {
            // This is a ROM bank select command.
            // Swap in the given ROM bank at 0x8000:
            this.loadRomBank(value, 0x8000);
        }
    }

    loadROM () {
        if (!this.nes.rom.valid) {
            alert("UNROM: Invalid ROM! Unable to load.");
            return;
        }

        // Load PRG-ROM:
        this.loadRomBank(0, 0x8000);
        this.loadRomBank(this.nes.rom.romCount - 1, 0xC000);

        // Load CHR-ROM:
        this.loadCHRROM();

        // Do Reset-Interrupt:
        this.nes.cpu.requestIrq(IRQType.RESET);
    }
}
